package com.demo.day716;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * @Auther: MFM
 * @Date: 2021/7/17 - 07 - 17 - 1:11
 * @version: 1.0
 */

public class Work2 {
    public static void subString(String value, int byteLength) {
        String result = "";
        for (int i = 0; i < value.length(); i++) {
            String str = String.valueOf(value.charAt(i));
            byte[] bts = str.getBytes();
            if (bts.length <= byteLength) {
                result = result.concat(str);
                byteLength = byteLength - bts.length;
            } else {
                break;
            }
        }
        System.out.println(result);
    }

    public static String Utf8(String str, int i) {
        //数组处理
        Array<Character> chars = new Array<>(str.length());
        for (int j = 0; j < str.length(); j++) {
            chars.addLast(str.charAt(j));
        }

        for (int j = 0; j < chars.getSize() - 1; j++) {
            boolean b = chars.get(j).equals(chars.get(j + 1));
            if (b) {
                throw new IllegalArgumentException("别连续输入一样的，要是这样爷不玩了");
            }
        }

        for (int j = 0; j < chars.getSize(); j++) {
            //当前字符
            char currentChar = chars.get(j);

            if (currentChar >= 'a' && currentChar <= 'z') {
            } else if (currentChar >= 'A' && currentChar <= 'Z') {
            } else if (currentChar >= '0' && currentChar <= '9') {
            } else {
                chars.add(j, chars.get(j));
                chars.add(j, chars.get(j));
                j += 2;
            }
        }

        int size = chars.getSize();
        for (int j = 0; j < size - i; j++) {
            chars.removeLast();
        }

        for (int j = 0; j < chars.getSize() - 3; j++) {
            if (chars.get(j) == chars.get(j + 1) && chars.get(j + 2) == chars.get(j + 1)) {
                chars.remove(j);
                chars.remove(j);
            }
        }


        char last = chars.get(chars.getSize() - 1);
        if (last >= 'a' && last <= 'z') {
        } else if (last >= 'A' && last <= 'Z') {
        } else if (last >= '0' && last <= '9') {
        } else {
            if (last == chars.get(chars.getSize() - 2) && chars.get(chars.getSize() - 3) == chars.get(chars.getSize() - 2)) {
                chars.removeLast();
                chars.removeLast();
            } else {
                if (last == chars.get(chars.getSize() - 2)) {
                    chars.removeLast();
                    chars.removeLast();
                } else {
                    chars.removeLast();
                }
            }
        }


        return String.valueOf(chars);
    }

    public static String Gbk(String str, int i) {
        //数组处理
        Array<Character> chars = new Array<>(str.length());
        for (int j = 0; j < str.length(); j++) {
            chars.addLast(str.charAt(j));
        }

        for (int j = 0; j < chars.getSize() - 1; j++) {
            boolean b = chars.get(j).equals(chars.get(j + 1));
            if (b) {
                throw new IllegalArgumentException("别连续输入一样的，要是这样爷不玩了");
            }
        }

        //使汉字占据字节个数组
        for (int j = 0; j < chars.getSize(); j++) {
            //当前字符
            char currentChar = chars.get(j);

            if (currentChar >= 'a' && currentChar <= 'z') {
            } else if (currentChar >= 'A' && currentChar <= 'Z') {
            } else if (currentChar >= '0' && currentChar <= '9') {
            } else {
                chars.add(j, chars.get(j));
                j++;
            }
        }

        boolean key = chars.get(i - 1) == chars.get(i);

        int size = chars.getSize();
        for (int j = 0; j < size - i; j++) {
            chars.removeLast();
        }


        for (int j = 0; j < chars.getSize() - 1; j++) {
            char currentChar = chars.get(j);
            if (currentChar >= 'a' && currentChar <= 'z') {
            } else if (currentChar >= 'A' && currentChar <= 'Z') {
            } else if (currentChar >= '0' && currentChar <= '9') {
            } else {
                if (chars.get(j) == chars.get(j + 1)) {
                    chars.remove(j);
                }
            }
        }


        if (key) {
            chars.removeLast();
        }


        return String.valueOf(chars);
    }

    public static void main(String[] args) {
        String str = "你2139吐acb了";
        System.out.println(Utf8(str, 10));
        System.out.println(Gbk(str, 10));
        subString(str, 10);
    }
}
